package ee.pri.pink.kallur;

import ee.pri.pink.kallur.gl.KallurGlView;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Process;
import android.os.Handler.Callback;

public class KallurActivity extends Activity {
	
	private HandlerThread sensorHandler;
	private Handler uiHandler;
	
	private static final int MSG_ROTATION = 1;
	
	private static final int SENSOR_RATE = SensorManager.SENSOR_DELAY_FASTEST;
	
	private RotationListener rotationListener = new RotationListener();
	
	KallurGlView kv;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(kv = new KallurGlView(this));
        uiHandler = new Handler(new Callback() {
			@Override
			public boolean handleMessage(Message msg) {
				switch (msg.what) {
        		case MSG_ROTATION:
        			float[] values = (float[])msg.obj;
        			kv.setRotation(values);
        			return true;
        		}
				return false;
			}
		});
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
        sensorHandler = new HandlerThread("SensorThread", Process.THREAD_PRIORITY_BACKGROUND) {
        	
        	private boolean quit = false, started = false;
        	
        	@Override
        	protected synchronized void onLooperPrepared() {
        		super.onLooperPrepared();

        		SensorManager manager = (SensorManager)getSystemService(SENSOR_SERVICE);
        		
        		Handler handler = new Handler(getLooper());
        		
        		if (!quit && !started) {
        			manager.registerListener(rotationListener, manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SENSOR_RATE, handler);
        			started = true;
        		}
        	}
        	
        	@Override
        	public synchronized boolean quit() {
        		if (started) {
        			SensorManager manager = (SensorManager)getSystemService(SENSOR_SERVICE);
        			manager.unregisterListener(rotationListener);
        			started = false;
        		}
        		quit = true;
        		return super.quit();
        	}
        };
        sensorHandler.start();
        kv.onResume();
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	sensorHandler.quit();
    	sensorHandler = null;
    	kv.onPause();
    }
	
	private class RotationListener implements SensorEventListener {
//		private int count = 0;
//		private float[] curValues = null;
		private long framerateThreshold = 16000000L;
		private long lastRotateUpdate = 0L;
		
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {}

		@Override
		public void onSensorChanged(SensorEvent event) {
// commented out smoothing, didn't work that well
//			if (count == 0) {
//				curValues = event.values;
//			} else {
//				for (int i = 0; i < curValues.length; i++) {
//					curValues[i] += event.values[i];
//				}
//			}
//			
//			count++;
			
			if (event.timestamp - lastRotateUpdate > framerateThreshold) {
				lastRotateUpdate = event.timestamp;
//				for (int i = 0; i < curValues.length; i++) {
//					curValues[i] /= (float)count;
//				}
//				count = 0;
				uiHandler.sendMessage(Message.obtain(uiHandler, MSG_ROTATION, event.values));
			}
		}
	}
}